package jse;

import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import jdk.nashorn.api.scripting.NashornScriptEngine;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;


@Configuration
@ComponentScan(basePackages = "jse")
@EnableWebMvc
@PropertySource("classpath:/jdbc.properties")
public class WebConfig implements WebMvcConfigurer {
	
	@Autowired
	Environment env;
	
	@Bean public DataSource dataSource() {
		System.err.println(env);
		HikariConfig config = new HikariConfig("/jdbc.properties");
    	HikariDataSource ds = new HikariDataSource(config);
    	return ds;
    }
    
    @Bean public CacheManager cacheManager() {return new CaffeineCacheManager();}
    
    @Bean public JdbcTemplate jdbcTemplate() {return new JdbcTemplate(dataSource());}
    
    @Bean public NashornScriptEngineFactory nashornFactory() {return new NashornScriptEngineFactory();}
    @Bean public NashornScriptEngine scriptEngine() {
    	return (NashornScriptEngine) nashornFactory().getScriptEngine("--language=es6","--optimistic-types=[true]");
    }
    @Bean(initMethod="init") public BeetlGroupUtilConfiguration beetlConfig() {return new BeetlGroupUtilConfiguration();}

	@Override//web 拦截器
    public void addInterceptors(InterceptorRegistry registry) {
//    	registry.addInterceptor(new JseInterceptor()).addPathPatterns("/**");
    }
	@Override  //静态化拦截器
	public void addResourceHandlers(ResourceHandlerRegistry registry) {  
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");  
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
        registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
    }
    @Override  
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {  
        configurer.ignoreAcceptHeader(true).defaultContentType(MediaType.TEXT_HTML)
        .mediaType("json", MediaType.APPLICATION_JSON)
        .mediaType("jsonp", MediaType.APPLICATION_JSON)
        .mediaType("xml", MediaType.APPLICATION_XML)
        .mediaType("htm", MediaType.TEXT_HTML)
        .mediaType("png", MediaType.IMAGE_PNG)
        .mediaType("jpg", MediaType.IMAGE_JPEG)
        .mediaType("jpeg", MediaType.IMAGE_JPEG)
        .mediaType("gif", MediaType.IMAGE_GIF)
        .mediaType("md", MediaType.TEXT_MARKDOWN)
        .mediaType("txt", MediaType.TEXT_PLAIN);
    }  
   
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	
	@Bean//beetl 视图
    public BeetlSpringViewResolver viewResolver() {
    	BeetlSpringViewResolver bsvr=new BeetlSpringViewResolver();
    	bsvr.setContentType("text/html;charset=UTF-8");
    	bsvr.setSuffix(".html");
    	return bsvr;
	}
	@Bean public MultipartResolver multipartResolver() {return new StandardServletMultipartResolver();}
}